//
//  HSSettingVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/3.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSSettingVC.h"

@interface HSSettingVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *isOn;

@end

@implementation HSSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.isOn = [[NSUserDefaults standardUserDefaults] objectForKey:NoPicKey];
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    NSArray<UITableViewCell *> *cells = [self.tableView visibleCells];
    __block NSString *isOn = @"0";
    [cells enumerateObjectsUsingBlock:^(UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 2) {
            UISwitch *sw = [obj viewWithTag:2333];
            isOn = sw.isOn ? @"1" : @"0";
        }
    }];
    [[NSUserDefaults standardUserDefaults] setObject:isOn forKey:NoPicKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [super viewDidDisappear:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 80, 5, 80, 34)];
        sw.onTintColor = HEXCOLOR(appNavColor);
        sw.tag = 2333;
        [cell addSubview:sw];
        [cell showLineForDirection:MYDirectionBottom leftPad:17];
    }
    cell.accessoryType = indexPath.row == 0 ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;
    UISwitch *sw = [cell viewWithTag:2333];
    sw.hidden = indexPath.row < 2;
    if (indexPath.row == 0) {
        cell.textLabel.text = @"版本号";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"V%@",[UIApplication sharedApplication].appVersion];
    }
    else if(indexPath.row == 1){
        YYImageCache *cache = [YYWebImageManager sharedManager].cache;
        // 获取当前缓存大小
        CGFloat dcost = cache.diskCache.totalCost / 1024. / 1024.;
        cell.textLabel.text = @"清除缓存";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",dcost];
    }
    else{
        cell.textLabel.text = @"2G/3G/4G网络为无图模式";
        cell.detailTextLabel.text = @" ";
        sw.on = [self.isOn isEqualToString:@"1"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 1) {
        YYImageCache *cache = [YYWebImageManager sharedManager].cache;
        CGFloat dcost = cache.diskCache.totalCost / 1024. / 1024.;
        if (dcost < 0.01) {
            [UIWindow showWarnMsg:@"没有缓存"];
        }
        else{
            // 清空内存缓存
            [cache.memoryCache removeAllObjects];
            [cache.diskCache removeAllObjects];
            @weakify(self);
            [cache.diskCache removeAllObjectsWithBlock:^{
                [[GCDQueue mainQueue] queueBlock:^{
                    @strongify(self);
                    [UIWindow showSuccessMsg:@"清除缓存成功"];
                    [self.tableView reloadData];
                }];
            }];
        }
    }
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
